package com.freesky.biz.icbc.service.impl;

import com.freesky.biz.icbc.domain.TFirstCustomer;
import com.freesky.biz.icbc.domain.TFirstCustomerConfig;
import com.freesky.biz.icbc.domain.TFirstCustomerVip;
import com.freesky.biz.icbc.mapper.FirstCustomerMapper;
import com.freesky.biz.icbc.service.IFirstCustomerService;
import com.freesky.biz.icbc.service.IcbcEquityService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FirstCustomerServiceImpl implements IFirstCustomerService {

    @Autowired
    private FirstCustomerMapper firstCustomerMapper;

    @Autowired
    private ITExportRecordService exportRecordService;



    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TFirstCustomer> findList(TFirstCustomer firstCustomer) {
        List<TFirstCustomer> firstCustomerList = firstCustomerMapper.findList(firstCustomer);
        for(int i=0;i<firstCustomerList.size();i++){
            TFirstCustomer firstCustomer1 = firstCustomerList.get(i);
            if("5001".equalsIgnoreCase(firstCustomer1.getCouponType())){
                String couponCode = firstCustomerMapper.getYonghuiCouponByBusiId(firstCustomer1.getOrderId());
                firstCustomer1.setExtra(couponCode);
            }
            firstCustomerList.set(i,firstCustomer1);
        }
        return firstCustomerList;
    }

    @Override
    public List<TFirstCustomerVip> selectVipList(TFirstCustomerVip entity) {
        return firstCustomerMapper.selectVipList(entity);
    }

    @Override
    public AjaxResult exportVipFile(TFirstCustomerVip entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("FIRST_CUSTOMER_VIP");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }
        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());

        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TFirstCustomerVip(), IFirstCustomerService.class,
                "selectVipList", "新客专享礼",Long.valueOf(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public AjaxResult exportFile(TFirstCustomer firstCustomer) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("FIRST_CUSTOMER");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }
        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());

        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(firstCustomer, new TFirstCustomer(), IFirstCustomerService.class,
                "findList", "新客礼",Long.valueOf(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }


    @Override
    public List<TFirstCustomerConfig> selectFirstCustomerConfig(List<String> activityList){
        return firstCustomerMapper.selectFirstCustomerConfig(activityList);
    }
}
